﻿CREATE PROCEDURE [dbo].[Share_Save]
	@userId UNIQUEIDENTIFIER,
	@title NVARCHAR(256),
	@items NVARCHAR(MAX)
AS
BEGIN
	DECLARE @shareId nvarchar(32)

	SET @shareId = CAST([dbo].[RandString](32) AS NVARCHAR(32))
	WHILE EXISTS (SELECT * FROM [dbo].[mark_Share] WHERE [ShareId] = @shareId)
	BEGIN
		SET @shareId = CAST([dbo].[RandString](32) AS NVARCHAR(32))
	END

	DECLARE @textId BIGINT, @notes NVARCHAR(MAX), @noteId BIGINT
	DECLARE @shareTextTable TABLE(
		TextId BIGINT
		,	NoteId BIGINT
	)

	DECLARE @CURSOR CURSOR
	DECLARE @NOTECURSOR CURSOR

	SET @CURSOR  = CURSOR SCROLL
	FOR
	SELECT CAST([key] AS BIGINT), [value] FROM [dbo].[SplitOnPairsStringByDelimiter](@items, N'#', N':')
	/*Открываем курсор*/
	OPEN @CURSOR
	/*Выбираем первую строку*/
	FETCH NEXT FROM @CURSOR INTO @textId, @notes
	/*Выполняем в цикле перебор строк*/
	WHILE @@FETCH_STATUS = 0
	BEGIN
		IF @notes IS NULL
		BEGIN
			INSERT INTO @shareTextTable (TextId, NoteId)
			VALUES (@textId, NULL)
			--INSERT INTO [dbo].[mark_Share_Text]
			--					 ([ShareId]
			--					 ,[TextId]
			--					 ,[NoteId])
			--VALUES
			--					 (@shareId		--<ShareId, nvarchar(64),>
			--					 ,@textId			--<TextId, bigint,>
			--					 ,NULL				--<NoteId, bigint,>
			--					 )
		END
		ELSE
		BEGIN
			SET @NOTECURSOR  = CURSOR SCROLL
			FOR
			SELECT  CAST([value] AS BIGINT) FROM [dbo].[SplitStringByDelimiter] (@notes, N';')
			/*Открываем курсор*/
			OPEN @NOTECURSOR
			/*Выбираем первую строку*/
			FETCH NEXT FROM @NOTECURSOR INTO @noteId
			/*Выполняем в цикле перебор строк*/
			WHILE @@FETCH_STATUS = 0
			BEGIN
				INSERT INTO @shareTextTable (TextId, NoteId)
				VALUES (@textId, @noteId)
				--INSERT INTO [dbo].[mark_Share_Text]
				--						([ShareId]
				--						,[TextId]
				--						,[NoteId])
				--VALUES
				--						(@shareId		--<ShareId, nvarchar(64),>
				--						,@textId		--<TextId, bigint,>
				--						,@noteId		--<NoteId, bigint,>
				--						)
			/*Выбираем следующую строку*/
			FETCH NEXT FROM @NOTECURSOR INTO @noteId
			END
			CLOSE @NOTECURSOR
		END
	/*Выбираем следующую строку*/
	FETCH NEXT FROM @CURSOR INTO @textId, @notes
	END
	CLOSE @CURSOR

	---------------------

	DECLARE /*@topTextId BIGINT,*/ @topTextIdChapterId UNIQUEIDENTIFIER, @topTextIdOrder INT

	SELECT TOP 1 /*@topTextId = tt.[TextId],*/ @topTextIdChapterId = t.[ChapterId], @topTextIdOrder = t.[Order]
	FROM @shareTextTable AS tt
	INNER JOIN [dbo].[text_Text] AS t ON t.[TextId] = tt.[TextId]
	ORDER BY t.[Order]

	SELECT TOP 1 @title = CAST(ISNULL([Value], @title) AS NVARCHAR(256))
	FROM [dbo].[text_Text]
	WHERE [ChapterId] = @topTextIdChapterId AND [Order] <= @topTextIdOrder AND [Tag] LIKE 'h%'
	ORDER BY [Order] DESC
/*
	DECLARE @bookTitle NVARCHAR(MAX)

	SELECT @bookTitle = [Title] 
	FROM [dbo].[text_Chapter]
	WHERE BookId = (SELECT [BookId] FROM [dbo].[text_Chapter] WHERE [ChapterId] = @topTextIdChapterId) AND ParentChapterId IS NULL
*/
	-----------------------


	INSERT INTO [dbo].[mark_Share]
          ([ShareId]
          ,[UserId]
          ,[Title])
  VALUES
          (@shareId			--<ShareId, nvarchar(64),>
          ,@userId			--<UserId, uniqueidentifier,>
          ,@title				--<Title, nvarchar(256),>
          )

	INSERT INTO [dbo].[mark_Share_Text] ([ShareId],[TextId],[NoteId])
	SELECT @shareId, [TextId],[NoteId] FROM @shareTextTable

	SELECT @shareId AS [ShareId], @title AS [Title]--, @bookTitle AS [BookTitle]

END